package com.cy.test;

import com.cy.mapper.UserMapper;
import com.cy.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class mybatistest {
    InputStream stream = null;
    SqlSessionFactory build = null;
    SqlSession sqlSession = null;
    UserMapper mapper = null;

    @Before
    public void testbefore () throws Exception {
        String s = "mybatis-config.xml";
        stream = Resources.getResourceAsStream(s);
        build = new SqlSessionFactoryBuilder().build(stream);
        sqlSession = build.openSession();
        mapper = sqlSession.getMapper(UserMapper.class);
    }

    @After
    public void testAfter () {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void selectAll (){
        List<User> users = mapper.selectAll();
//        users.forEach(item -> System.out.println(item)); //
        users.forEach(System.out::println); //只能在list  用

    }

    @Test
    public void insertUser (){
        int i = mapper.insertUser();
        System.out.println(i);
        selectAll();
        //数据库的事务(原子性,隔离性,持久性,一致性)
    }

    @Test
    public void updateUser(){
        int i = mapper.updateUser();
        System.out.println(i);
        selectAll();
    }

    @Test
    public void deleteUser(){
        int i = mapper.deleteUser();
        System.out.println(i);
        selectAll();
    }

    @Test
    public void updateUser22(){
        int i = mapper.updateUser2(28);
        System.out.println(i);
    }


        @Test
    public void updateUser3(){
        int i = mapper.updateUser3(28,"不摇碧莲");
        System.out.println(i);
        selectAll();
    }

}
